<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class M_ppk extends CI_Model {

	public function __construct() {
		parent::__construct();
	}

	function get() {
		$sql = 'SELECT kd_ppk, ppk, jenis_ppk, kelas_ppk
		 FROM ppk mppk  
		 LEFT JOIN master_jenis_ppk jppk ON mppk.id_jenis_ppk=jppk.id_jenis_ppk
		 LEFT JOIN master_kelas_ppk kppk ON mppk.id_kelas_ppk=kppk.id_kelas_ppk
		 ORDER BY kelas_ppk,ppk';
		return $sql;
	}

	function cek($rule) {
		$sql = 'SELECT * FROM ppk WHERE ppk=? OR kd_ppk=?';
		$query = $this->db->query($sql, $rule);
        return $query;
	}

	function cekUpdate($rule) {
		$sql = 'SELECT * FROM ppk WHERE (ppk=? OR kd_ppk=?) AND kd_ppk<>?';
		$query = $this->db->query($sql, $rule);
        return $query;
	}

	function get_cari($keyword='') {
		$sql = 'SELECT kd_ppk, ppk, jenis_ppk, kelas_ppk
		 FROM ppk mppk  
		 LEFT JOIN master_jenis_ppk jppk ON mppk.id_jenis_ppk=jppk.id_jenis_ppk
		 LEFT JOIN master_kelas_ppk kppk ON mppk.id_kelas_ppk=kppk.id_kelas_ppk
		 WHERE ppk like "%'.$keyword.'%" OR kd_ppk like "%'.$keyword.'%" 
		 OR jenis_ppk like "%'.$keyword.'%" OR kelas_ppk like "%'.$keyword.'%"
		 ORDER BY kelas_ppk,ppk';
		return $sql;
	}

	function get_export() {
		$sql = 'SELECT kd_ppk, ppk, jenis_ppk, kelas_ppk, alamat, direktur
		 FROM ppk mppk  
		 LEFT JOIN master_jenis_ppk jppk ON mppk.id_jenis_ppk=jppk.id_jenis_ppk
		 LEFT JOIN master_kelas_ppk kppk ON mppk.id_kelas_ppk=kppk.id_kelas_ppk
		 ORDER BY kelas_ppk,ppk';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_ket($params) {
		$sql = 'SELECT ppk FROM ppk WHERE kd_ppk = ?';
		$query = $this->db->query($sql, $params);
		if ($query->num_rows() > 0) {
			$result = $query->row_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result['ppk'];
	}

	function getJenis(){
		$sql = 'SELECT * FROM master_jenis_ppk';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function getKelas(){
		$sql = 'SELECT * FROM master_kelas_ppk';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function save($dataInsert) {
		$query = $this->db->insert('ppk', $dataInsert);
		return $query;
	}

	function get_by($params) {
		$sql = 'SELECT kd_ppk, ppk, jenis_ppk, mppk.id_jenis_ppk as JENIS, mppk.id_kelas_ppk AS KELAS,
		 kelas_ppk, alamat, direktur
		 FROM ppk mppk  
		 LEFT JOIN master_jenis_ppk jppk ON mppk.id_jenis_ppk=jppk.id_jenis_ppk
		 LEFT JOIN master_kelas_ppk kppk ON mppk.id_kelas_ppk=kppk.id_kelas_ppk
		 WHERE mppk.kd_ppk = ?';
		$query = $this->db->query($sql, $params);
		if ($query->num_rows() > 0) {
			$result = $query->row_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function update($kd_ppk, $dataUpdate) {
		$this->db->where('kd_ppk', $kd_ppk);
		$update = $this->db->update('ppk', $dataUpdate);
		return $update;
	}

	function delete($kd_ppk) {
		$this->db->where('kd_ppk', $kd_ppk);
		$delete = $this->db->delete('ppk');
		return $delete;
	}
}
